package com.hyk.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.entity.User;
import com.hyk.entity.request.UserRequest;
import com.hyk.enums.ApiEnum;
import com.hyk.exception.GlobalException;
import com.hyk.mapper.UserMapper;
import com.hyk.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyk.utils.JwtUtils;
import com.hyk.utils.MD5Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hyk
 * @since 2021-03-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public void register(UserRequest request) {
        if (StringUtils.isBlank(request.getUsername()) || StringUtils.isBlank(request.getPassword())){
            throw new GlobalException(ApiEnum.USERNAME_OR_PASSWORD_IS_NULL);
        }

        //判断用户是否存在
        List<User> users = userMapper.selectByUserName(request.getUsername());
        if (users.size()>0){
            throw new GlobalException(ApiEnum.USERNAME_ALREADY_EXIST);
        }

        //密码加密
        String encode = MD5Utils.encode(request.getPassword());

        User user = new User();
        BeanUtils.copyProperties(request,user);
        user.setPassword(encode);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.insert(user);
    }

    @Override
    public Map<String, Object> login(UserRequest request) {
        //账号密码是否为空
        if (StringUtils.isBlank(request.getUsername()) || StringUtils.isBlank(request.getPassword())){
            throw new GlobalException(ApiEnum.USERNAME_OR_PASSWORD_IS_NULL);
        }
        //验证码是否为空
        if (StringUtils.isBlank(request.getUuid()) || StringUtils.isBlank(request.getVerificationCode())){
            throw new GlobalException(ApiEnum.UUID_IS_NULL);
        }
        //获取验证码
        Object obj = redisTemplate.opsForValue().get(request.getUuid());
        if (obj == null){
            throw new GlobalException(ApiEnum.VERIFICATION_CODE_ERROR);
        }

        String code = (String) obj;
        if (!request.getVerificationCode().equalsIgnoreCase(code)){
            throw new GlobalException(ApiEnum.VERIFICATION_CODE_ERROR);
        }

        List<User> users = userMapper.selectByUserName(request.getUsername());
        if (users.size()!=1){
            throw new GlobalException(ApiEnum.USERNAME_OR_PASSWORD_IS_ERROR);
        }
        User user = users.get(0);

        String encode = MD5Utils.encode(request.getPassword());
        if (!user.getPassword().equals(encode)){
            throw new GlobalException(ApiEnum.USERNAME_OR_PASSWORD_IS_ERROR);
        }

        //生成令牌
        Map<String,Object> map = new HashMap<>();
        map.put("user",user);
        String token = JwtUtils.createJWT(user.getId().toString(),map,user.getUsername(),900);
        //构造返回结果
        Map<String,Object> result = new HashMap<>();
        result.put("user",user);
        result.put("token",token);
        redisTemplate.delete(request.getUuid());
        return result;

    }

    @Override
    public IPage<User> findListForPage(UserRequest request) {
        IPage<User> userIPage = new Page<>(request.getPage(),request.getSize());
        IPage<User> user = userMapper.listPage(userIPage,request);
        return user;
    }
}
